import pandas as pd
import numpy as np
from emtencent import config as cn
import json
import time
import tushare as ts
from urllib.request import urlopen, Request
import re
import sys
import datetime



def get_tc_concept_list():
    req = Request(cn.CONCEPT_LIST)
    text = urlopen(req, timeout=10).read()
    js = _handle_module_code_text(text)
    del js['bd_cpt']
    df = pd.DataFrame(js)
    df = df.T
    return df



# 获取概念分类
def get_tc_concept_classified(pause=0.01):
    df = get_tc_concept_list()
    dic = df.to_dict(orient='index')
    stk_basic = ts.get_stock_basics()
    stk_basic_dic = stk_basic.to_dict(orient='index')
    l = []
    for k, v in dic.items():
        time.sleep(pause)
        try:
            m = get_tc_concept_code_list(k, base_code_dic=stk_basic_dic, base_concept_dic=dic)
            l.extend(m)
        except:
            print('Time out Error ,Concept Id:%s' % k)
            continue
        sys.stdout.write('#' + "\b\b")
        sys.stdout.flush()
    df_concept = pd.DataFrame(l, columns=['code', 'name', 'concept_code', 'concept_name'])
    return df_concept


def get_tc_concept_code_list(concept_id, base_code_dic=None, base_concept_dic=None):
    dic = base_concept_dic if base_concept_dic else get_tc_concept_list().to_dict(orient='index')
    stk_basic_dic = base_code_dic if base_code_dic else ts.get_stock_basics().to_dict(orient='index')
    if concept_id:
        req = Request(cn.STKS_WITH_CONCEPT % concept_id)
        text = urlopen(req, timeout=10).read()
        text = text.decode('utf-8')
        l2 = re.compile(r'(sz|sh)((\d){6})').findall(text)
        m = [[name[1], (stk_basic_dic[name[1]])['name'], concept_id, dic[concept_id]['name']] for name in l2 if
             name[1] in stk_basic_dic.keys()]
    return m

def get_tc_concept_code_df(concept_id, base_code_dic=None, base_concept_dic=None):
    l = get_tc_concept_code_list(concept_id, base_code_dic, base_concept_dic)
    df = pd.DataFrame(l, columns=['code', 'name', 'concept_code', 'concept_name'])
    df = df.drop_duplicates(subset=['code'], keep='first')
    return df


def get_tc_industry_list():
    req = Request(cn.INDUSTRY_LIST)
    text = urlopen(req, timeout=10).read()
    js = _handle_module_code_text(text)
    del js['bd_ind']
    df = pd.DataFrame(js)
    df = df.T
    return df


def get_tc_industry_classified(pause=0.01):
    df = get_tc_industry_list()
    dic = df.to_dict(orient='index')
    stk_basic = ts.get_stock_basics()
    stk_basic_dic = stk_basic.to_dict(orient='index')
    l = []
    for k, v in dic.items():
        time.sleep(pause)
        try:
            m = get_tc_industry_code_list(k, base_code_dic=stk_basic_dic, base_industry_dic=dic)
            l.extend(m)
        except:
            print('Time out Error ,Concept Id:%s' % k)
            continue
        sys.stdout.write('#' + "\b\b")
        sys.stdout.flush()
    df_concept = pd.DataFrame(l, columns=['code', 'name', 'industry_code', 'industry_name'])
    return df_concept


def get_tc_industry_code_list(concept_id, base_code_dic=None, base_industry_dic=None):
    dic = base_industry_dic if base_industry_dic else get_tc_industry_list().to_dict(orient='index')
    stk_basic_dic = base_code_dic if base_code_dic else ts.get_stock_basics().to_dict(orient='index')
    if concept_id:
        req = Request(cn.STKS_WITH_TC_INDUSTRY % concept_id)
        text = urlopen(req, timeout=10).read()
        text = text.decode('utf-8')
        l2 = re.compile(r'(sz|sh)((\d){6})').findall(text)
        m = [[name[1], (stk_basic_dic[name[1]])['name'], concept_id, dic[concept_id]['name']] for name in l2 if
             name[1] in stk_basic_dic.keys()]
    return m

def _handle_module_code_text(text):
    text = text.decode('GBK')
    text = text.replace("bd0", "0").replace("SS_pt", "")
    text = text.replace("\"t\"", "\"name\"").replace("\"clk\"", "\"code\"").replace(",\"pt\":\"bd_cpt\",\"chd\":[]","").replace(",\"pt\":\"bd_ind\",\"chd\":[]","").replace("\"idx\":3,", "")
    js = json.loads(text)
    return js

def _handele_module_detail_data(text):
    text = text.decode('GBK')



# type 1 2 3 分别表示行业 概念 地域
def get_tc_top_least_code(type=1, max_count=5, need_module_detail=True):
    # 下面的接口是直接拿排序
    req = Request(cn.MODULE_SORT_LIST%type)
    text = urlopen(req, timeout=10).read()
    l = _handle_module_sort_list_text(text)
    l = l[:max_count]
    if need_module_detail:
        return get_tc_module_detail_data(type=type,module_list=l)
    return None


def get_tc_module_detail_data(type=1,module_list=[]):
    param = ','.join(module_list)
    req = Request(cn.MODULE_DETAIL_DATA%param)
    text = urlopen(req, timeout=10).read()
    text = text.decode('GBK')
    result = re.compile(r'v_bkhz\d{6}=\"(.*)\";').findall(text)
    source = ['Unknown', 'tc_industry', 'tc_concept', 'tc_area']
    if len(result) == len(module_list):
        # '"012057~水上运输~6~1~3~10~3174.95~99.34~3.23~3844890.00~249112.00~sh601919~sh603167~102928.73~63353.23~39575.49~65723.96~16.0"
        # (后面还有些未知的)代码~板块名~上涨家数~平盘数~下跌家数~公司总数~平均价格~涨跌额~涨跌幅~总成交量(手)~总成交额(万)~领涨股~领跌股~总流入~总流出~净流入
        l = []
        index = []
        for x in result:
            item = x.split('~')
            if len(item) == 18:
                item = item[0:16]
                l.append(item)
                index.append(item[0])
            # d = {'module_code':item[0], 'module_name':item[1], 'up_count':item[2], 'p_count':item[3], 'down_count':item[4],
            #      'total_count':item[5], 'avg':item[6], 'zde':item[7], 'zdf':item[8],
            #      'total_cj_count':item[9], 'total_cj_money':item[10],
            #      'lz_g':item[11], 'ld_g':item[12]}
        df = pd.DataFrame(data=l, columns=['module_code', 'module_name', 'up_count', 'p_count', 'down_count',
                                            'total_count', 'avg', 'zde', 'zdf', 'total_cj_count',
                                           'total_cj_money', 'top_zf_gu', 'top_df_gu','inamount','outamount','netamount'],
                          index=index)
        df['top_zf_gu'] = df['top_zf_gu'].str.replace('sz', '').str.replace('sh', '')
        df['top_df_gu'] = df['top_df_gu'].str.replace('sz', '').str.replace('sh', '')
        # 上涨股占比
        df['up_pct'] = pd.to_numeric(df['up_count'])/pd.to_numeric(df['total_count'])
        # 下跌股占比
        df['down_pct'] = pd.to_numeric(df['down_count'])/pd.to_numeric(df['total_count'])
        # 净流入率
        df['netamout_pct'] = pd.to_numeric(df['netamount'])/pd.to_numeric(df['inamount'])
        df['zdf'] = pd.to_numeric(df['zdf'])/100
        df['source'] = source[type]
        df['last_update_time'] = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        return df


def _handle_module_sort_list_text(text):
    text = text.decode('GBK')
    # bkqt 在获取具体板块实时行情的时候都会变成bkhz
    text = text.replace("var list_data=", '').replace(";", "").replace("bkqt", "bkhz").replace("t:", "\"t\":").replace("p:","\"p\":").replace("total:","\"total\":").replace("l:","\"l\":").replace("o:","\"o\":").replace("data:","\"data\":").replace('\'','\"')
    js = json.loads(text)
    return js['data'].split(',')


# get_tc_concept_list()

# get_tc_concept_classified()

# get_tc_industry_list()

# get_tc_industry_classified()

# get_tc_top_least_code(type=2)


# from emmodels.em_base_data import ModuleData
# def caculate_cur_hot():
#     for i in [1]:
#         df = get_tc_top_least_code(type=i, max_count=100)
#         for index, item in df.iterrows():
#             module = ModuleData(**(item.to_dict()))
#             module.saveToMongo()
# caculate_cur_hot()


pass
